import axios from 'axios'

const baseURL = '/api/v1'

const service = axios.create({
  baseURL,
  timeout: 5000,
})

service.interceptors.request.use(
  (config) => {
    // 如果有token 就携带tokon
    const token = window.localStorage.getItem('accessToken')
    if (token) {
      config.headers.common.Authorization = token
    }
    return config
  },
  (error) => Promise.reject(error)
)

service.interceptors.response.use(
  (response) => {
    const res = response.data
    if (res.code != 2000) {
      return Promise.reject(new Error(res.mes || 'Erro'))
    } else {
      return res
    }
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default service
